package com.bwie.gddproject.index.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.gddproject.R;
import com.bwie.gddproject.index.model.bean.UserBean;
import com.bwie.gddproject.index.view.XiaoXiActivity;
import com.bwie.gddproject.login.model.LoginBean;
import com.bwie.gddproject.login.view.LoginActivity;
import com.bwie.gddproject.net.NetUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by 我走路带风 on 2017/11/9.
 */

public class MyFragment extends Fragment implements View.OnClickListener {
    private View view;
    private SimpleDraweeView mTouXiang;
    /**
     * 登录/注册>
     */
    private TextView mLogin;
    private ImageView mSz;
    private ImageView mXiaoxi;
    private RelativeLayout mLl;
    private ImageView mTz;
    private SharedPreferences user;
    private Bundle arguments;
    private String username;
    private int uid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.my_fragment, null);
        initView(view);
        EventBus.getDefault().register(this);
        user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);

        if (user.getString("name","")==null ||user.getString("name","")=="" ){
                Log.e("tag","asdf");
        }else{
            String uid = user.getString("uid", "");
            NetUtil.getResponseService().getUserInfo(uid +"")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<UserBean>() {


                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(UserBean userBean) {
                            username = userBean.getData().getUsername();
                            mLogin.setText(username);
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }

        return view;
    }

    private void initView(View view) {
        mTouXiang = (SimpleDraweeView) view.findViewById(R.id.tou_xiang);
        mTouXiang.setOnClickListener(this);
        mLogin = (TextView) view.findViewById(R.id.login);
        mLogin.setOnClickListener(this);
        mSz = (ImageView) view.findViewById(R.id.sz);
        mSz.setOnClickListener(this);
        mXiaoxi = (ImageView) view.findViewById(R.id.xiaoxi);
        mXiaoxi.setOnClickListener(this);
        mLl = (RelativeLayout) view.findViewById(R.id.ll);
        mTz = (ImageView) view.findViewById(R.id.tz);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tou_xiang:
                break;
            case R.id.login:
                user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
                if (user.getString("name","")==null || user.getString("name","")== ""){
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivityForResult(intent,1);
                }else{
                   Toast.makeText(getActivity(),"您已登陆,您的用户名为"+username+"...",Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.sz:
                break;
            case R.id.xiaoxi:

                user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
                if (user.getString("name","")==null || user.getString("name","")== ""){
                    Toast.makeText(getActivity(),"请登录您的账号...",Toast.LENGTH_SHORT).show();
                }else{
                    Intent intent = new Intent(getActivity(), XiaoXiActivity.class);
                    startActivity(intent);
                }


                break;
        }
    }
    @Subscribe
    public void getMessage(LoginBean loginBean){
        uid = loginBean.getData().getUid();
            user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putString("uid", uid + "");
        editor.commit();
        NetUtil.getResponseService().getUserInfo(uid + "")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBean userBean) {
                        mLogin.setText(userBean.getData().getUsername());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == 1){
//            LoginBean loginBean = (LoginBean) data.getSerializableExtra("loginBean");
//            uid = loginBean.getData().getUid();
//            user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor=user.edit();
//            editor.putString("uid", uid+"");
//            editor.commit();
//            NetUtil.getResponseService().getUserInfo(uid+"")
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe(new Observer<UserBean>() {
//                        @Override
//                        public void onSubscribe(Disposable d) {
//
//                        }
//
//                        @Override
//                        public void onNext(UserBean userBean) {
//                            mLogin.setText(userBean.getData().getUsername());
//                        }
//
//                        @Override
//                        public void onError(Throwable e) {
//
//                        }
//
//                        @Override
//                        public void onComplete() {
//
//                        }
//                    });
//
//        }
//    }
}
